<template>
	<view class="container">
		<loading :show="loading_show"></loading>
		<!-- 个人地址 -->
		<scroll-view scroll-y="true" class="list_scroll">
			<view class="list_box">
				<view class="label box_shadow" @click.stop="selectAddressTap(index)" v-for="(item, index) in addressList" :key="index">
					<view class="addressInfo">
						<view class="title">
							<view class="name">{{item.name}}</view>
							<view class="mobbile">{{item.mobile}}</view>
							<view class="defaultIcon" v-if="item.default">默认地址</view>
						</view>
						<view class="addressText">{{item.province}}{{item.city}}{{item.district}}{{item.address}}{{item.houseNumber}}</view>
						<view class="editIcon"  v-if="!manageFlag">
							<aicon v-if="addressIndex === index" type="roundcheckfill" :color="_temp_themeColor" size="25"></aicon>
							<aicon v-else type="round" color="#bebebe" size="25"></aicon>
						</view>
						<view class="editIcon" @click.stop="editCurrAddress(index)" v-else>
							<u-icon name="edit-pen" :color="_temp_themeColor" size="24"></u-icon>
						</view>
					</view>
					<view class="manageBox" v-if="manageFlag">
						<view class="left" @click="selectToDefault(index)">
							<view class="changeDefault">
								<aicon v-if="item.default" type="roundcheckfill" :color="_temp_themeColor" size="18"></aicon>
								<aicon v-else type="round" color="#bebebe" size="18"></aicon>
							</view>	
							<view class="selectDefText">
								<text>设为默认地址</text>
							</view>
						</view>
						<view class="right" @click="deleteAddress(index)">
							<text>删除</text>
						</view>
					</view>
				</view>
				<view class="emptyGoods" v-if="addressList.length===0">
					<u-empty
						text="暂无地址信息"
						mode="data"
						icon="http://cdn.uviewui.com/uview/empty/data.png"
					>
					</u-empty>
				</view>
			</view>
		</scroll-view>
		<view class="footer1">
			<view class="label">
				<view class="button" :style="{backgroundColor: _temp_themeColor}" @click="addressEnter">确认</view>
			</view>
		</view>
	</view>
</template>

<script>
	import { updateUserInfo } from "@/tools/request/api.js"
	import AddressEdit from './address-edit.vue'
	import { mapState, mapMutations, mapGetters, mapActions } from 'vuex'
	export default {
		name:"address-list",
		components: {
			AddressEdit
		},
		props: {
			manageFlag: {
				type: Boolean,
				default: false
			}
		},
		data() {
			return {
				addressIndex: 0,
				loading_show: false,
				title: '',
				editCurrentAddress: {},
				edit_show: false,
			};
		},
		mounted () {
			this.setCurrentAddress();
		},
		computed: {
			...mapState('app_store', ['currentAddress']),
		},
		watch: {
			currentAddress (newVal, oldVal) {
				this.addressIndex = 0;
			}
		},
		methods: {
			...mapMutations('app_store', ['SET_CURRENT_ADDRESS']),
			// 勾选地址
			selectAddressTap (index) {
				this.addressIndex = index;
			},
			// 确认地址
			addressEnter () {
				this.SET_CURRENT_ADDRESS(this.addressList[this.addressIndex]);
				this.$bus.$emit('selectClose');
			},
			// 设置当前选择的地址
			setCurrentAddress () {
				this.addressList.forEach((item, index) => {
					if (item.order === this.currentAddress.order) {
						this.addressIndex = index;
					}
				})
			},
			// 设置默认地址
			selectToDefault (index) {
				this.loading_show = true;
				let address_list = Array.from(this.addressList);
				address_list.forEach((item, index1) => {
					if (index === index1) {
						item.default = true;
					} else {
						item.default = false;
					}
				})
				let postData = {
					receiveAddressJson: JSON.stringify(address_list)
				};
				updateUserInfo(postData).then(res => {
					this.loading_show = false;
					if (res.code != 200) return uni.$showMsg('修改失败');
					uni.$showMsg('修改成功');
					this.getUserInfo();
					this.$emit('setManage');
				});
			},
			// 删除地址
			deleteAddress (index) {
				uni.showModal({
					title: '提示',
					content: '确定要删除吗?',
					success: (res) => {
						if (!res.confirm) return;
						let deletedList = this.addressList;
						deletedList.splice(index, 1);
						let postData = {
							"receiveAddressJson": JSON.stringify(deletedList)
						}
						updateUserInfo(postData).then(res => {
							if(res.code === 200){
								uni.$showMsg('地址删除成功');
								this.getUserInfo();
								this.$emit('setManage');
							}else{
								uni.showModal({
									content: res.mag,
									showCancel: false
								});
							}
						})
					}
				});
			},
			// 编辑地址
			editCurrAddress (index) {
				this.$emit('editItemClick', index);
			},
		}
	}
</script>

<style lang="scss" scoped>
	.container{
		width: 100%;
			
		.list_scroll{
			height: 55vh;
			
			.list_box{
				
				.label{
					padding: 0 20rpx;
					margin: 20rpx 0;
					background-color: #fff;
					
					.addressInfo{
						padding: 40rpx 0;
						position: relative;
						
						.title{
							display: flex;
							align-items: center;
							padding-bottom: 20rpx;
							
							.name{
								font-weight: bold;
								color: #1c1c1c;
								font-size: 32rpx;
								padding: 0 6rpx;
							}
							
							.mobbile{
								font-size: 28rpx;
								color: #1c1c1c;
								padding-right: 10rpx;
							}
							
							.defaultIcon{
								padding: 0 4rpx;
								border-radius: 4rpx;
								border: 1rpx solid #F9AE3D;
								color: #F9AE3D;
								font-size: 24rpx;
								transform: rotate(360deg);
							}
						}
						.addressText{
							width: 85%;
							color: #666;
							font-size: 28rpx;
							white-space: normal;
							overflow: hidden;
							text-overflow: ellipsis;
						}
					}
					
					.manageBox{
						display: flex;
						justify-content: space-between;
						padding: 20rpx 0;
						align-items: center;
						border-top: 1rpx solid #b6b6b6;
						
						.left{
							display: flex;
							align-items: center;
							font-size: 26rpx;
							color: #666;
							
							.changeDefault{
								display: flex;
								align-items: center;
							}
							.selectDefText{
								display: flex;
								align-items: center;
								padding-left: 6rpx;
							}
						}
						.right{
							font-size: 26rpx;
							color: #666;
						}
						
					}
					.editIcon{
						position: absolute;
						right: 20rpx;
						top: 50%;
						transform: translateY(-50%);
					}
				}
			}
		}
		.footer1{
			width: 100%;
			
			.label{
				display: flex;
				align-items: center;
				justify-content: space-between;
				height: 120rpx;
				width: 100%;
				
				.button{
					width: 80%;
					margin: 0 auto;
					height: 80rpx;
					border-radius: 100rpx;
					color: #fff;
					font-size: 32rpx;
					display: flex;
					align-items: center;
					justify-content: center;
					background-color: #71beea;
				}
			}
		}
	}
</style>